#include "cTexture.h"

GLuint cTexture::GetTextureId()
{
	return m_textureId;
}

void cTexture::LoadTexture(char *url)
{
	char* textureData = LoadTGA(url, &m_width, &m_height, &m_bit);

	//Check texture data 
	if (textureData == NULL) 
	{
		esLogMessage("Load TGA fail!");
		return;
	}

	glGenTextures(1, &m_textureId);
	glBindTexture(GL_TEXTURE_2D, m_textureId);

	glTexImage2D(GL_TEXTURE_2D, 0, (m_bit == 24?GL_RGB:GL_RGBA), m_width, m_height, 0, (m_bit == 24?GL_RGB:GL_RGBA), GL_UNSIGNED_BYTE, textureData);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	SAFE_DEL_ARRAY(textureData);
}
void cTexture::LoadSkyDomeTexture(char* bottom, char* top, char* front, char* back, char* left, char* right)
{
	// Generate a texture object 
	glGenTextures(1, &m_textureId); 
 
	glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureId); 

	char* texture_Bottom = LoadTGA(bottom, &m_width, &m_height, &m_bit);
	char* texture_Top = LoadTGA(top, &m_width, &m_height, &m_bit);
	char* texture_Back = LoadTGA(back, &m_width, &m_height, &m_bit);
	char* texture_Front = LoadTGA(front, &m_width, &m_height, &m_bit);
	char* texture_Left = LoadTGA(left, &m_width, &m_height, &m_bit);
	char* texture_Right = LoadTGA(right, &m_width, &m_height, &m_bit);

	// Load the cube face - Positive X 
	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_Top); 
 
	// Load the cube face - Negative X 
	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_Right); 
 
	// Load the cube face - Positive Y 
	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_Left); 
 
	// Load the cube face - Negative Y 
	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_Bottom); 
 
	// Load the cube face - Positive Z 
	glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_Back); 
 
	// Load the cube face - Negative Z 
	glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGB, m_width, m_height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture_Front);

	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);

	SAFE_DEL_ARRAY(texture_Bottom);
	SAFE_DEL_ARRAY(texture_Top);
	SAFE_DEL_ARRAY(texture_Back);
	SAFE_DEL_ARRAY(texture_Front);
	SAFE_DEL_ARRAY(texture_Left);
	SAFE_DEL_ARRAY(texture_Right);
}